package hos.kotlin

/**
 * <p>Title: ColorExt </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2023/3/30 20:55
 * @version : 1.0
 */


//<editor-fold desc="Color">
/**
 * Set the alpha component of `color` to be `alpha`.
 *
 * @param color The color.
 * @param alpha Alpha component \([0..255]\) of the color.
 * @return the `color` with `alpha` component
 */
fun Int.withAlpha(
 alpha: Int
): Int {
 return this and 0x00ffffff or (alpha shl 24)
}

/**
 * Set the alpha component of `color` to be `alpha`.
 *
 * @param color   The color.
 * @param opacity Opacity component \([0..1]\) of the color.
 * @return the `color` with `alpha` component
 */
fun Int.withOpacity(
 opacity: Float
): Int {
 return this and 0x00ffffff or ((opacity * 255.0f + 0.5f).toInt() shl 24)
}


/**
 * Set the red component of `color` to be `red`.
 *
 * @param color The color.
 * @param red   Red component \([0..255]\) of the color.
 * @return the `color` with `red` component
 */
fun Int.withRed(
 red: Int
): Int {
 return this and -0xff0001 or (red shl 16)
}

/**
 * Set the red component of `color` to be `red`.
 *
 * @param color The color.
 * @param red   Red component \([0..1]\) of the color.
 * @return the `color` with `red` component
 */
fun Int.withRed(
 red: Float
): Int {
 return this and -0xff0001 or ((red * 255.0f + 0.5f).toInt() shl 16)
}

/**
 * Set the green component of `color` to be `green`.
 *
 * @param color The color.
 * @param green Green component \([0..255]\) of the color.
 * @return the `color` with `green` component
 */
fun Int.withGreen(
 green: Int
): Int {
 return this and -0xff01 or (green shl 8)
}

/**
 * Set the green component of `color` to be `green`.
 *
 * @param color The color.
 * @param green Green component \([0..1]\) of the color.
 * @return the `color` with `green` component
 */
fun Int.withGreen(
 green: Float
): Int {
 return this and -0xff01 or ((green * 255.0f + 0.5f).toInt() shl 8)
}

/**
 * Set the blue component of `color` to be `blue`.
 *
 * @param color The color.
 * @param blue  Blue component \([0..255]\) of the color.
 * @return the `color` with `blue` component
 */
fun Int.withBlue(
 blue: Int
): Int {
 return this and -0x100 or blue
}

/**
 * Set the blue component of `color` to be `blue`.
 *
 * @param color The color.
 * @param blue  Blue component \([0..1]\) of the color.
 * @return the `color` with `blue` component
 */
fun Int.withBlue(
 blue: Float
): Int {
 return this and -0x100 or (blue * 255.0f + 0.5f).toInt()
}

/**
 * Color-int to color-string.
 *
 * @param colorInt The color-int.
 * @return color-string
 */
fun Int.int2RgbString(): String {
 val colorInt = this and 0x00ffffff
 var color = Integer.toHexString(colorInt)
 while (color.length < 6) {
  color = "0$color"
 }
 return "#$color"
}

/**
 * Color-int to color-string.
 *
 * @param colorInt The color-int.
 * @return color-string
 */
fun Int.int2ArgbString(): String {
 var color = Integer.toHexString(this)
 while (color.length < 6) {
  color = "0$color"
 }
 while (color.length < 8) {
  color = "f$color"
 }
 return "#$color"
}
//</editor-fold>

